<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="Content-Language" content="en" />
    <title>skalibs: the alarm library interface</title>
    <meta name="Description" content="skalibs: the alarm library interface" />
    <meta name="Keywords" content="skalibs c unix alarm getitimer setitimer timer_create timer_gettime timer_settime library libstddjb" />
    <!-- <link rel="stylesheet" type="text/css" href="//skarnet.org/default.css" /> -->
  </head>
<body>

<p>
<a href="index.html">libstddjb</a><br />
<a href="../libskarnet.html">libskarnet</a><br />
<a href="../index.html">skalibs</a><br />
<a href="//skarnet.org/software/">Software</a><br />
<a href="//skarnet.org/">skarnet.org</a>
</p>

<h1> The <tt>alarm</tt> library interface </h1>

<p>
 The following functions are declared in the <tt>skalibs/alarm.h</tt> header,
and implemented in the <tt>libskarnet.a</tt> or <tt>libskarnet.so</tt> library.
</p>

<h2> General information </h2>

<p>
 <tt>alarm</tt> is a set of primitives to provide the same functionality as
<a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/alarm.html">alarm()</a>,
but with sub-second precision.
</p>

<p>
 Depending on the functionality the underlying system provides,
the precision can be 1 nanosecond (implementation via
<a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/timer_getoverrun.html">timer_settime()</a>,
1 microsecond (implementation via
<a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/setitimer.html">setitimer()</a>,
or 1 second (fallback implementation with
<a href="http://pubs.opengroup.org/onlinepubs/9699919799/functions/alarm.html">alarm()</a>
when nothing better can be found).
</p>

<h2> Functions </h2>

<p>
<code> int alarm_milliseconds (unsigned int n) </code> <br />
Sets a fuse that will raise a SIGALRM after <em>n</em> milliseconds.
If <em>n</em> is 0, the SIGALRM will be raised instantly.
Returns 1 on success and 0 (and sets errno) on failure.
</p>

<p>
<code> int alarm_timeout (tain_t const *tto) </code> <br />
Sets a fuse that will raise a SIGALRM after some amount
of time has passed. The amount of time is described in
*<em>tto</em>, which is a relative
<a href="tai.html">tain_t</a>, i.e. a structure containing
a relative TAIN64 time.
Returns 1 on success and 0 (and sets errno) on failure.
</p>

<p>
<code> int alarm_deadline (tain_t const *deadline) </code> <br />
Sets a fuse that will raise a SIGALRM when the clock reaches
*<em>deadline</em>, which is an absolute time expressed in
<a href="tai.html">TAI64N</a> format.
Returns 1 on success and 0 (and sets errno) on failure.
</p>

<p>
<code> void alarm_disable (void) </code> <br />
Cancels a previously set fuse. No SIGALRM will be raised.
</p>

<h2> Notes </h2>

<ul>
 <li> Asynchronous programming via signals is bad. The best way
to handle situations where something happens after some time has
elapsed is to use an asynchronous loop primitive such as
<a href="iopause.html">iopause()</a>. The problem is that some
external libraries only provide synchronous functions (including
functions talking to the network!) with no obvious way to set a
timeout. The <tt>alarm_*</tt> set of functions is meant to work
around that, with hopefully better granularity than the POSIX
<tt>alarm()</tt> function. </li>
</ul>

</body>
</html>
